#ifndef __I2C_H
#define __I2C_H

#include "util.h"


/**
    * @brief Initializes the structure of i2c parameters
    */
typedef struct
{
	u32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.
                                         This parameter must be set to a value lower than 400kHz */  
}  I2C_InitDef;

/**
    * @brief  structure of i2c parameters
    */
typedef struct
{
	u8_t Ctrl; /*!<  0:automatic dma address increment after each read/write
				1: restart between write and read*/
	u8_t Scll;/*!< SCL low clock count*/
	u8_t Sclh;/*!< SCL high clock count*/
	u8_t Stsu;/*!< SDA setup clock count at start*/
	u8_t Sthd;/*!< SDA hold clock count at start */
	u8_t Sosu;/*!< SDA setup clock count at stop*/
	u8_t Dtsu;/*!< SDA setup clock count for data*/
	u8_t Dthd;/*!< SDA hold clock count for data*/
}  I2C_RegDef;

#define IICD_CLOCKSPEED200KHZ 26
#define IICD_CLOCKSPEED400KHZ 11
#define IICD_CLOCKSPEED800KHZ 4
#define IICD_CLOCKSPEED1MHZ 2


#define MAX_IICD_WRITE_LENGTH			0x20
#define MAX_IICD_READ_LENGTH			0x20



#define I2C_REGADDR8BITS(x)	(0x01000000|x)
#define I2C_REGADDR16BITS(x)	(0x10000000|x)


#define IS_I2C_REGADDR(x)			(0x11000000&x)
#define IS_I2C_REGADDR8BITS(x)	(0x01000000&x)
#define IS_I2C_REGADDR16BITS(x)	(0x10000000&x)
#define DEV_ADDR_EEPROM	  0x50
#define REG_ADDR_EEPROM	  0x3000
#define MPU9250_ADDRESS   0x69
#define MAG_ADDRESS       0x0C

/**
  * @brief I2C Initializes function
  * @param i2c_initstruct : Initialization struct only include baudrate
  * @return null
  */
void I2C_init(I2C_InitDef* i2c_initstruct);

/**
  * @brief Writing a array to i2c bus
  * @param  dev_addr : device address of target
  * @param  reg_addr : register address of target
  * @param  len : number of bytes written
  * @param  pbuff : the pointer of data
  * @return null
  */
void I2C_WriteDataFromBuff(u8_t dev_addr,u32_t reg_addr,u16_t len, u8_t* pbuff);

/**
  * @brief Read data from i2c bus 
  * @param 1 dev_addr : Device address of target
  * @param 2 reg_addr : Register address of target
  * @param 3 len : Number of bytes read from i2c bus
  * @param 4 pbuff : The address where the read data is stored
  * @return null
  */
void I2C_ReadDataToBuff(u8_t dev_addr, u32_t reg_addr, u16_t len, u8_t* pbuff);





#endif
